/*
 * Copyright ShangHai ECLUB Information Technology Co.,Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 * http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.eclub.emotion.rootstone.server.core.communication.http;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.HttpRequest;

import com.eclub.emotion.rootstone.protocol.Protocol;
import com.eclub.emotion.rootstone.protocol.sdp.RequestProtocol;
import com.eclub.emotion.rootstone.server.context.RsChannel;
import com.eclub.emotion.rootstone.server.context.RsContext;
import com.eclub.emotion.rootstone.server.context.ServerType;
import com.eclub.emotion.rootstone.server.core.DefaultInvocationHandler;
import com.eclub.emotion.rootstone.server.core.InvocationHandler;
import com.eclub.emotion.rootstone.server.http.mapping.RequestHandlerMapping;
import com.eclub.emotion.rootstone.server.http.mapping.ServiceMethodHandler;


/**
 *  A Handler which provides an individual handler method
 * for each event type.  This handler down-casts the received upstream event
 * into more meaningful sub-type event and calls an appropriate handler method
 * with the down-cast event. the handler is NETTY based.
 * 
 * @author   jun.yang, Wincor Nixdorf International GmbH
 */
public class NettyRequestTransport extends SimpleChannelUpstreamHandler{
	
	private InvocationHandler invocationHandler;
	
	private RequestHandlerMapping mapping = new RequestHandlerMapping();
	
	public NettyRequestTransport(){
		invocationHandler = new DefaultInvocationHandler();
		try {
			mapping.initialize();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private com.eclub.emotion.rootstone.server.http.HttpRequest prepareRequest(MessageEvent evt) {
		HttpRequest nettyHttpRequest = (HttpRequest) evt.getMessage();
		return RequestUtils.newHttpRequest(nettyHttpRequest);
	} 
	
	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent evt) throws Exception {
		
		
		com.eclub.emotion.rootstone.server.http.HttpRequest request = prepareRequest(evt);
		ServiceMethodHandler serviceMethodHandler = mapping.getHandler(request);
		if(serviceMethodHandler==null){
			throw new Exception("Not found handler to handle this request.");
		}
		request.setFromIP(evt.getChannel().getRemoteAddress().toString());
		request.setToIP(evt.getChannel().getLocalAddress().toString());
		request.setServiceMethodHandler(serviceMethodHandler);
		RsContext rsContext = new RsContext();
		rsContext.setServerType(ServerType.HTTP);
		rsContext.setChannel(new RsChannel(evt.getChannel()));
		rsContext.setHttpResuest(request);
		Protocol protocol = new Protocol();
		RequestProtocol requestProtocol = new RequestProtocol();
		protocol.setSdpEntity(requestProtocol);
		rsContext.setProtocol(protocol);
		
		invocationHandler.invoke(rsContext);
	}
	
	
}


